<?php

namespace App\Http\Controllers\Storage;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\Models\Storage\Appear;
use Illuminate\Support\Facades\DB;
use App\Models\Storage\Enter;

class StatisticsController extends Controller
{
    protected $rules = [];

    /**
     * Create a new GoodsController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('refresh.token');
    }

    public function enter(Request $request)
    {
        $limit = $request->get('limit') ? $request->get('limit') : 20;
        $sort = $request->get('sort') ? $request->get('sort') : 'created_at';
        $params = [];
        if ($request->get('EBELN')) $params[] = ['EBELN',  'like','%'. $request->get('EBELN')];
        if ($request->get('PRODUCTCD')) $params[] = ['PRODUCTCD',  'like','%'. $request->get('PRODUCTCD')];
        if ($request->get('LGORT')) $params[] = ['LGORT',  'like','%'. $request->get('LGORT')];

        $result = Enter::where($params)->with(['product'])->orderBy($sort, 'desc')->paginate($limit);
        return sendData(200, '请求成功', $result);
    }

    public function appear(Request $request)
    {
        $limit = $request->get('limit') ? $request->get('limit') : 20;
        $sort = $request->get('sort') ? $request->get('sort') : 'created_at';
        $params = [];
        if ($request->get('VBELN')) $params[] = ['VBELN',  'like','%'. $request->get('VBELN')];
        if ($request->get('PRODUCTCD')) $params[] = ['PRODUCTCD',  'like','%'. $request->get('PRODUCTCD')];

        $result = Appear::where($params)->with(['product'])->orderBy($sort, 'desc')->paginate($limit);
//        $outStc = DB::table('ord_out_ensure')->select('VBELN')->get();
//            foreach ($result as $k => $item) {
//                $data[] = [
//                    'VBELN' => $item->VBELN,
//                ];
//            }
//        foreach ($data as $v){
//            $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
//            $temp[]=$v;
//        }
//        $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
//        $ord = DB::table('ord_out_dirt as o')
//                ->select('o.OrderNo','o.OutStcNo','c.ShopSignNM','o.AdmQnty','o.PRODUCTCD','p.PRODCHINM')
//                ->leftJoin('customer as c','c.CUSTOMERCD','=','o.CustomerCd')
//                ->leftJoin('product as p','p.PRODUCTCD','=','o.PRODUCTCD')
//                ->whereIn('OutStcNo',$temp)
////                ->tosql();
////        dd($ord);
//                ->get();
//        dd($ord);
//        $adj = DB::table('adj_out_dirt')->whereIn('OutStcNo',$temp)->get();
//        $data = [];
//        foreach ($ord as $item => $key) {
//            dd($key);
//            $data = [
//              'OrderNo'=>$key->OrderNo,
//              'OutStcNo'=>$key->OutStcNo,
//              'ShopSignNM'=>$key->ShopSignNM,
//              'OrderNo'=>$key->OrderNo,
//            ];
//        }
//        dd($data);

        return sendData(200, '请求成功', $result);
    }

}